package StackDemo;

import java.lang.reflect.Array;
import java.util.Arrays;

class TestDemo1{
    public static void main(String[] args) {
        ArrayStack as = new ArrayStack(10);
        as.push(1);
        as.push(2);
        as.push(3);
        as.push(4);
        as.push(5);

        try {
            as.pop();
            as.pop();
            as.pop();
            as.pop();
            as.pop();
            as.pop();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

public class ArrayStack {
    private final int capacity;
    private int top = -1;// 栈顶: 起始值设为-1(0)都可以
    private int[] stack ;

    public ArrayStack(int capacity){
        this.capacity = capacity;
        stack = new int[capacity];
    }

    // 入栈
    public void push(int data){
        if(isFull()){
            stack = Arrays.copyOf(stack,capacity*2);
        }
        top++;
        stack[top] = data;
    }

    // 出栈
    public void pop(){
        if(isEmpty()){
            throw new StackEmptyException("队列为空,无法删除元素");
        }
        System.out.println(stack[top]);
        top--;
    }

    public boolean isFull(){
        return top == capacity - 1;
    }
    public boolean isEmpty(){
        return top == - 1;
    }
}
